package com.acegear.horizon.domain.repository.jpa;

import com.acegear.horizon.domain.models.constraint.FeedType;
import com.acegear.horizon.domain.models.jpa.ClubFeed;

import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by wuwf on 17/4/14.
 * clud的feed流
 */
public interface ClubFeedRepository extends PagingAndSortingRepository<ClubFeed, Long> {
    /**
     * 根据position从大到小查询某个club的类型为feedType的集合(top时例外，按updateAt)
     */
    List<ClubFeed> findByClubIdAndFeedTypeAndTop(Long clubId, FeedType feedType, boolean top, Pageable pageable);

    /**
     * 根据position从大到小查询某个club的feed集合(top时例外，按updateAt)
     */
    List<ClubFeed> findByClubIdAndTop(Long clubId, boolean top, Pageable pageable);

    /**
     * 查询除了post的所有其他内容feed
     */
    List<ClubFeed> findByClubIdAndTopAndFeedTypeNot(Long clubId, boolean top, FeedType feedType, Pageable pageable);

    /**
     * 根据类型和关联的ID删除某项
     */
    @Modifying
    @Transactional
    void deleteByClubIdAndRelateIdAndFeedType(Long clubId, Long relateId, FeedType feedType);

    /**
     * 查询某个club最大的position，供新增时加1
     */
    @Query(value = "select max(position) from ClubFeed where clubId = ?1", nativeQuery = true)
    Integer findMaxPosition(Long clubId);

    ClubFeed findByClubIdAndRelateIdAndFeedType(Long clubId, Long relateId, FeedType feedType);
}
